﻿namespace Access.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using Access;
    using BaseClass.Duoc;
    using System;
    using System.Data;

    public class DKhaiBaoTonKho
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _conn;
        private General _xuli;
        public int pLoaiXuat;

        public DKhaiBaoTonKho()
        {
            this.pLoaiXuat = 0;
            this._condata = new ConnectDatabase();
            this._xuli = new General(this._condata);
        }

        public DKhaiBaoTonKho(ConnectDatabase condata)
        {
            this.pLoaiXuat = 0;
            this._condata = condata;
            this._xuli = new General(this._condata);
        }

        private string f_getsql_khaibaotonct(string tungay, string denngay)
        {
            string str5;
            string str = "";
            if (tungay != "")
            {
                str5 = str;
                str = str5 + " and to_timestamp(to_char(b." + dtoDKhaiBaoTonKhoCT.columnNGAY + ",'dd/mm/yyyy hh24:mi'),'dd/mm/yyyy hh24:mi')>=to_timestamp('" + tungay + "','dd/mm/yyyy hh24:mi')";
            }
            if (denngay != "")
            {
                str5 = str;
                str = str5 + " and to_timestamp(to_char(b." + dtoDKhaiBaoTonKhoCT.columnNGAY + ",'dd/mm/yyyy hh24:mi'),'dd/mm/yyyy hh24:mi')<=to_timestamp('" + denngay + "','dd/mm/yyyy hh24:mi')";
            }
            string str2 = this._condata.Schema + ".";
            return ("select a." + dtoDDMDuocPham.columnID + " as idsp,a." + dtoDDMDuocPham.columnTEN + " as tensp,a." + dtoDDMDuocPham.columnMA + " as masp,a." + dtoDDMDuocPham.columnDVT + " as dvt,a." + dtoDDMDuocPham.columnMAVACH + " as mavach,a2." + dtoDDMNhaSanXuat.columnTEN + " as tennhasx,b." + dtoDKhaiBaoTonKhoCT.columnIDDANGNHAP + " as iddangnhap,to_char(b." + dtoDKhaiBaoTonKhoCT.columnNGAY + ",'dd/mm/yyyy hh24:mi') as ngay,case when b." + dtoDKhaiBaoTonKhoCT.columnSLTONDAU + " is null then 0 else b." + dtoDKhaiBaoTonKhoCT.columnSLTONDAU + " end as sltondau,case when b." + dtoDKhaiBaoTonKhoCT.columnDONGIA + " is null then 0 else b." + dtoDKhaiBaoTonKhoCT.columnDONGIA + " end as dongia,case when b2." + dtoDNhapKhoCT.columnSOLUONGXUAT + " is null then 0 else b2." + dtoDNhapKhoCT.columnSOLUONGXUAT + " end as soluongxuat,case when b." + dtoDKhaiBaoTonKhoCT.columnID + " is null then 0 else b." + dtoDKhaiBaoTonKhoCT.columnID + " end as id from " + str2 + dtoDDMDuocPham.tableName + " a left join " + str2 + dtoDKhaiBaoTonKhoCT.tableName + " b on a." + dtoDDMDuocPham.columnID + "=b." + dtoDKhaiBaoTonKhoCT.columnIDSANPHAM + " left join " + str2 + dtoDNhapKhoCT.tableName + " b2 on b." + dtoDKhaiBaoTonKhoCT.columnID + "=b2." + dtoDNhapKhoCT.columnID + " left join " + str2 + dtoDDMNhaSanXuat.tableName + " a2 on a." + dtoDDMDuocPham.columnIDNHASX + "=a2." + dtoDDMNhaSanXuat.columnID + ((str != "") ? (" where 1=1 " + str) : "") + " order by b." + dtoDKhaiBaoTonKhoCT.columnNGAYCN + " desc");
        }

        public bool f_insert_tonkho(dtoDKhaiBaoTonKhoCT chitiet)
        {
            dtoDNhapKhoCT oct;
            dtoDNhapKho kho;
            bool flag;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                kho = new dtoDNhapKho(this._xuli.pConFigData) {
                    pIDDANGNHAP = chitiet.pIDDANGNHAP,
                    pLOAINHAP = HangSo.LoaiNhapKho.enLoaiNhapKho.TaiNhap.GetHashCode(),
                    pNGAY = chitiet.pNGAY,
                    pNGAYHD = chitiet.pNGAY.Substring(0, 10),
                    pTENMAYTINH = chitiet.pTENMAYTINH
                };
                oct = new dtoDNhapKhoCT(this._xuli.pConFigData) {
                    pGIABAN = chitiet.pDONGIA,
                    pGIAMUA = chitiet.pDONGIA,
                    pGIANHAP = chitiet.pDONGIA,
                    pIDDANGNHAP = kho.pIDDANGNHAP,
                    pIDDUOCPHAM = chitiet.pIDSANPHAM,
                    pSOLUONG = chitiet.pSLTONDAU,
                    pSOLUONG1 = chitiet.pSLTONDAU,
                    pSOLUONG2 = 1M,
                    pTENMAYTINH = chitiet.pTENMAYTINH
                };
                kho.pID = long.Parse(this._xuli.f_Get_CapID());
                if (!kho.Insert())
                {
                    transaction.Rollback();
                    return false;
                }
                oct.pID = chitiet.pID;
                oct.pIDNHAPKHO = kho.pID;
                if (!(chitiet.Insert(this._conn) && oct.Insert(this._conn)))
                {
                    kho.Delete();
                    chitiet.pID = 0L;
                    transaction.Rollback();
                    return false;
                }
                transaction.Commit();
                flag = true;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                kho = null;
                oct = null;
                this._conn.Close();
                this._conn = null;
            }
            return flag;
        }

        public DataTable f_loaddata_khaibaotonkho(string ngaygiokiemke)
        {
            DataTable table = new DataTable();
            string sql = this.f_getsql_khaibaotonct(ngaygiokiemke, ngaygiokiemke);
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public DataTable f_loaddata_khaibaotonkho_theongay(string tungay, string denngay)
        {
            DataTable table = new DataTable();
            tungay = tungay.Substring(0, 10) + " 00:00";
            denngay = denngay.Substring(0, 10) + " 23:59";
            string sql = "select distinct ngay from (" + this.f_getsql_khaibaotonct(tungay, denngay) + ") a";
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public bool f_update_tonkho(dtoDKhaiBaoTonKhoCT chitiet)
        {
            dtoDNhapKhoCT oct;
            dtoDNhapKho kho;
            bool flag;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                kho = new dtoDNhapKho(this._xuli.pConFigData) {
                    pIDDANGNHAP = chitiet.pIDDANGNHAP,
                    pLOAINHAP = HangSo.LoaiNhapKho.enLoaiNhapKho.TaiNhap.GetHashCode(),
                    pNGAY = chitiet.pNGAY,
                    pNGAYHD = chitiet.pNGAY.Substring(0, 10),
                    pTENMAYTINH = chitiet.pTENMAYTINH
                };
                oct = new dtoDNhapKhoCT(this._xuli.pConFigData) {
                    pGIABAN = chitiet.pDONGIA,
                    pGIAMUA = chitiet.pDONGIA,
                    pGIANHAP = chitiet.pDONGIA,
                    pIDDANGNHAP = kho.pIDDANGNHAP,
                    pIDDUOCPHAM = chitiet.pIDSANPHAM,
                    pSOLUONG = chitiet.pSLTONDAU,
                    pSOLUONG1 = chitiet.pSLTONDAU,
                    pSOLUONG2 = 1M,
                    pTENMAYTINH = chitiet.pTENMAYTINH,
                    pID = chitiet.pID
                };
                dtoDNhapKhoCT oct2 = new dtoDNhapKhoCT(this._condata) {
                    pID = oct.pID
                };
                oct2.Load();
                kho.pID = oct2.pIDNHAPKHO;
                oct.pSOLUONGXUAT = oct2.pSOLUONGXUAT;
                oct.pIDNHAPKHO = kho.pID;
                if (!kho.Update())
                {
                    transaction.Rollback();
                    return false;
                }
                if (!(chitiet.Update(this._conn) && oct.Update(this._conn)))
                {
                    transaction.Rollback();
                    return false;
                }
                transaction.Commit();
                flag = true;
            }
            catch
            {
                flag = false;
            }
            finally
            {
                kho = null;
                oct = null;
                this._conn.Close();
                this._conn = null;
            }
            return flag;
        }
    }
}

